package com.wolf.etl.core.rule.convert;

import com.wolf.etl.core.ETLManager;
import com.wolf.etl.enumeration.CONVERT_RULE_TYPE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by tommy's father on 2017/8/25.
 */
public abstract class AbstractConvertRule {

    private static Logger logger = LoggerFactory.getLogger(AbstractConvertRule.class);

    private static final String logger_prefix = "wolf-etl#com.wolf.etl.core.rule.convert#AbstractConvertRule";

    public abstract RetInfo convert(String cxt, String src, String... params);

    private static final Object lock = new Object();
    private ETLManager manager;

    public static class RetInfo {
        private String srouceInfo;
        private String targetInfo;

        public RetInfo() {
        }

        public RetInfo(String srouceInfo, String targetInfo) {
            this.srouceInfo = srouceInfo;
            this.targetInfo = targetInfo;
        }

        public String getSrouceInfo() {
            return srouceInfo;
        }

        public void setSrouceInfo(String srouceInfo) {
            this.srouceInfo = srouceInfo;
        }

        public String getTargetInfo() {
            return targetInfo;
        }

        public void setTargetInfo(String targetInfo) {
            this.targetInfo = targetInfo;
        }
    }


    public static AbstractConvertRule getInstance(CONVERT_RULE_TYPE ruleType, ETLManager manager) {
        synchronized (lock) {
            try {
                Class<?> clazz = Class.forName(String.format("%s.%s",getPackageName(), ruleType.getConvertRuleType()));
                AbstractConvertRule checkRule = (AbstractConvertRule) manager.getBeanManager().createBean(clazz);
                checkRule.manager = manager;
                return checkRule;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static final String getPackageName() {

        return new Object() {
            public String getClassForStatic() {
                return this.getClass().getPackage().getName();
            }
        }.getClassForStatic();
    }
}
